﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class product_insurance_user_insurance_accident_claim : System.Web.UI.Page
{
     private ClaimBLL bll = new ClaimBLL();

     private const String FILE_PATH = "~/upload/insurance/";

     private bool haveDeathError = false;

     private bool haveDisabilityError = false;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
     

    protected void btn_claim_death_Click(object sender, EventArgs e)
    {
        if (haveDeathError) return;
        String docPath1 = fileUpload(fu_ad1);
        String docPath2 = fileUpload(fu_ad2);
        String docPath3 = fileUpload(fu_ad3);
        String docPath4 = fileUpload(fu_ad4);


        if (!Util.IsBlank(docPath1))
        {
            docPath1 = FILE_PATH + docPath1;
        }
        if (!Util.IsBlank(docPath2))
        {
            docPath2 = FILE_PATH + docPath2;
        }
        if (!Util.IsBlank(docPath3))
        {
            docPath3 = FILE_PATH + docPath3;
        }
        if (!Util.IsBlank(docPath4))
        {
            docPath4 = FILE_PATH + docPath4;
        }



        string userId = Session["userId"] as String;
        int insuredID = int.Parse(Request.QueryString["certificateNo"]);
        String insurance_acc = new AccountsManager().getInsuranceAccountNo(userId, "A");

        bll.newClaim(insurance_acc, "", "PENDING", 0, DateTime.Now, "", insuredID, "Personal Accident Claim Form", docPath1, "Death Certificate", docPath2, " Autopsy Report", docPath3, " Letter of Administration", docPath4, "", "", "", "");
        this.Response.Redirect("./insurance_after_submit.aspx");  
    }
    protected void btn_claim_permanentDisablement_Click(object sender, EventArgs e)
    {
        if (haveDisabilityError) return;
        String docPath1 = fileUpload(fu_adp1);
        String docPath2 = fileUpload(fu_adp2);
        String docPath3 = fileUpload(fu_adp3);

        if (!Util.IsBlank(docPath1))
        {
            docPath1 = FILE_PATH + docPath1;
        }
        if (!Util.IsBlank(docPath2))
        {
            docPath2 = FILE_PATH + docPath2;
        }
        if (!Util.IsBlank(docPath3))
        {
            docPath3 = FILE_PATH + docPath3;
        }
        

        string userId = Session["userId"] as String;
        int insuredID = int.Parse(Request.QueryString["certificateNo"]);
        String insurance_acc = new AccountsManager().getInsuranceAccountNo(userId, "A");

        bll.newClaim(insurance_acc, "", "PENDING", 0, DateTime.Now, "",insuredID, "Personal Accident Claim Form", docPath1, "Medical Report", docPath2, "Medical Bills", docPath3, "", "", "", "", "", "");
        this.Response.Redirect("./insurance_after_submit.aspx");
    }


    

    private String getFileNamePrefix()
    {
        string userId = Session["userId"] as String;
        int caseID = (new AccountsManager().getMaxValue("CLAIM", "caseID")) + 1;
        String fileName = userId + "_" + caseID + "_";


        return fileName;
    }



    private String fileUpload(FileUpload fileUpload)
    {
        string filename = "";
        if (fileUpload.HasFile)
        {
            try
            {
                filename = getFileNamePrefix() + fileUpload.FileName;
                fileUpload.SaveAs(Server.MapPath(FILE_PATH) + filename);

            }
            catch (Exception ex)
            {
                //TODO StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }

        return filename;
    }

    protected void vd_File1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        String docPath1 =  fileUpload(fu_ad1);
        String docPath2 =  fileUpload(fu_ad2);
        String docPath3 =  fileUpload(fu_ad3);
        String docPath4 =  fileUpload(fu_ad4);

        if (Util.IsInputBlank(docPath1) &&
            Util.IsInputBlank(docPath2) &&
            Util.IsInputBlank(docPath3) &&
            Util.IsInputBlank(docPath4)
            )
        {
            args.IsValid = false;
            haveDeathError = true;
        }
        else
        {
            args.IsValid = true;
            haveDeathError = false;
        }
    }
    protected void vd_File2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        String docPath1 = fileUpload(fu_adp1);
        String docPath2 = fileUpload(fu_adp2);
        String docPath3 = fileUpload(fu_adp3);
        

        if (Util.IsInputBlank(docPath1) &&
            Util.IsInputBlank(docPath2) &&
            Util.IsInputBlank(docPath3) 
           
            )
        {
            args.IsValid = false;
            haveDisabilityError = true;
        }
        else
        {
            args.IsValid = true;
            haveDisabilityError = false;
        }
    }
    protected void btn_reset1_Click(object sender, EventArgs e)
    {
       
      
    }


}